package com.management.controller;


import com.management.commons.exception.AccountException;
import com.management.commons.exception.ExceptionCode.AccountExceptionCode;
import com.management.controller.form.AccountForm;
import com.management.controller.form.AddAccountForm;
import com.management.controller.form.ChangeAccountForm;
import com.management.controller.form.GetAccountByNameForm;
import com.management.service.AccountService;
import com.management.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Api(tags = "用户相关")
@RestController
@RequestMapping("/account")
public class AccountController {

    @Resource
    private AccountService accountService;

    /**
     * 登录失败跳转至
     * @return
     */
    @ApiOperation("登录失败跳转至")
    @GetMapping("/loginerror")
    public Result loginerror(){
        return Result.getInstance().setCode(403).setMessage("请登录");
    }

    /**
     * 分页查询所有用户
     * @param accountForm 分页参数
     * @return
     */
    @ApiOperation("分页查询所有用户")
    @PostMapping("/getAllAccount")
    public Result getAllAccount(@RequestBody AccountForm accountForm){
       Result result= accountService.findAllAccount(accountForm);
       return result;
    }

    /**
     * 修改账号信息
     * @param changeAccountForm 接收前端参数
     * @return
     */
    @ApiOperation("修改账号信息")
    @PostMapping("changeAccountMessage")
    public Result changeAccountMessage(@RequestBody @Valid ChangeAccountForm changeAccountForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
        throw new AccountException(AccountExceptionCode.PARAMETERS_ERROR);
        }
        System.out.println(changeAccountForm);
       Result result= accountService.changeMessage(changeAccountForm);
        return result;}

    /**
     * 添加新员工
     * @param addAccountForm 相关参数
     * @return
     */
    @ApiOperation("添加新员工")
    @PostMapping("addAccount")
        public Result saveAccount(@RequestBody @Valid AddAccountForm addAccountForm){
       Result result= accountService.addAccount(addAccountForm);

        return result;
        }

    /**
     * 根据姓名模糊查询员工
     * @param
     * @return
     */
    @ApiOperation("根据姓名模糊查询员工")
    @PostMapping("getByName")
        public Result getByName(@RequestBody GetAccountByNameForm getAccountByNameForm){

       Result result= accountService.findByAccountName(getAccountByNameForm);
       return result;
        }
}

